package com.library.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.common.base.ApiResult;
import com.library.common.entity.MyPage;
import com.library.common.utils.PageUtil;
import com.library.entity.*;
import com.library.entity.dto.ResourceAuditDto;
import com.library.mapper.ResourceAuditMapper;
import com.library.service.ResourceAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.service.ResourceHomeService;
import com.library.service.ResourceService;
import com.library.service.UserInfoService;
import com.library.util.MoJiCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 资源审核中间表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2025-02-18
 */
@Service
public class ResourceAuditServiceImpl extends ServiceImpl<ResourceAuditMapper, ResourceAudit> implements ResourceAuditService {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ResourceHomeService resourceHomeService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public MyPage<ResourceAudit> queryRecords(ResourceAuditDto dto) {
        LambdaQueryWrapper<ResourceAudit> wrapper = Wrappers.lambdaQuery();
        if(ObjectUtil.isNotEmpty(dto.getAuditStatus())){
            String[] stringArray = dto.getAuditStatus().split(",");
            wrapper.in(ResourceAudit::getAuditStatus,Arrays.asList(stringArray));
        }
        wrapper.eq(ResourceAudit::getOpenId,dto.getOpenId());
        wrapper.orderByDesc(ResourceAudit::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<ResourceAudit> resourceEntities=this.list(wrapper);

        PageInfo<ResourceAudit> info = new PageInfo<>(resourceEntities);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public ResourceAudit detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("id不能为空,请核查!");
        }
        return this.getById(id);
    }

    @Override
    public boolean add(ResourceAudit resourceAudit) {
        resourceAudit.setAuditStatus("0");
        resourceAudit.setStatus("0");
        resourceAudit.setCreateTime(new Date());
        resourceAudit.setCreateId(MoJiCommonUtils.getUserId());
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getOpenId,resourceAudit.getOpenId());
        UserInfo userInfo=userInfoService.getOne(wrapper);
        if(userInfo==null)
        {
            LambdaQueryWrapper<UserInfo> wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(UserInfo::getUserId,resourceAudit.getOpenId());
            UserInfo userInfo1=userInfoService.getOne(wrapper1);
            if (userInfo1 != null) {
                resourceAudit.setProvince("41");
                resourceAudit.setCity(userInfo1.getCityCode());
                resourceAudit.setArea(userInfo1.getAreaCode());
                resourceAudit.setOpenId(userInfo1.getOpenId());
            }
        }
        if(userInfo !=null){
            resourceAudit.setProvince("41");
            resourceAudit.setCity(userInfo.getCityCode());
            resourceAudit.setArea(userInfo.getAreaCode());
        }
        return this.save(resourceAudit);
    }

    @Override
    public boolean edit(ResourceAudit resourceAudit) {
        return this.updateById(resourceAudit);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("id不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean release(Long id) {
        ResourceAudit resourceAudit=this.getById(id);
        resourceAudit.setStatus("1");
        resourceAudit.setAuditStatus("4");
        this.updateById(resourceAudit);
        ResourceEntity resourceEntity=new ResourceEntity();
        BeanUtil.copyProperties(resourceAudit,resourceEntity);
        resourceEntity.setSource("app");
        resourceEntity.setSfRecommend(7);
        resourceEntity.setPublishTime(new Date());
        resourceEntity.setCreateTime(LocalDateTime.now());
        resourceEntity.setSecondType(resourceAudit.getCity());
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ResourceEntity::getSfRecommend,"7");
        wrapper.orderByDesc(ResourceEntity::getPageView);
        wrapper.orderByDesc(ResourceEntity::getLikeCount);
        wrapper.last("LIMIT 1");
        ResourceEntity resourceEntity1 = resourceService.getOne(wrapper);
//        resourceEntity.setPageView(resourceEntity1.getPageView()+1);
        resourceEntity.setPageView(0L);
        resourceEntity.setResourceAuditId(resourceAudit.getResourceId());
        //向首页资源表同时插入数据
        ResourceHome resourceHome=new ResourceHome();
        BeanUtil.copyProperties(resourceAudit,resourceHome);
        resourceHome.setSource("app");
        resourceHome.setSfRecommend(7);
        resourceHome.setPublishTime(new Date());
        resourceHome.setCreateTime(LocalDateTime.now());
        resourceHome.setSecondType(resourceAudit.getCity());

//        resourceEntity.setPageView(resourceEntity1.getPageView()+1);
        resourceHome.setPageView(0L);
        resourceHome.setResourceAuditId(resourceAudit.getResourceId());
        resourceHomeService.save(resourceHome);
        return resourceService.save(resourceEntity);
    }

}
